package ducna.misscall.screen.main;

/**
 * @author DUCNA
 */
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import ducna.alozo.misscall.R;
import ducna.misscall.common.BaseActivity;
import ducna.misscall.screen.main.adapter.MainFragmentPagerAdapter;

public class MainActivity extends BaseActivity implements OnTabChangeListener,
		OnPageChangeListener {

	private TabHost mTabHost;
	private ViewPager mViewPager;
	private MainFragmentPagerAdapter pagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
		mViewPager = (ViewPager) findViewById(R.id.viewPager);

		pagerAdapter = new MainFragmentPagerAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(pagerAdapter);

		createNewTab(mTabHost, getString(R.string.title_miss_call), getString(R.string.title_miss_call), R.drawable.icon_miss_call_selected);
		createNewTab(mTabHost, getString(R.string.title_about), getString(R.string.title_about), R.drawable.icon_about);
		createNewTab(mTabHost, getString(R.string.title_contact), getString(R.string.title_contact), R.drawable.icon_contact_unselected);
		createNewTab(mTabHost, getString(R.string.title_setting), getString(R.string.title_setting), R.drawable.icon_setting_unselected);
		
		mViewPager.setOnPageChangeListener(this);
		mTabHost.setOnTabChangedListener(this);

	}

	private void createNewTab(TabHost tabHost, String tag, String title, int iconId) {
		TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
		tabSpec.setIndicator(getTabIndicator(tabHost.getContext(), title, iconId));
		tabSpec.setContent(new TabFactory(this));
		tabHost.addTab(tabSpec);
	}

	class TabFactory implements TabContentFactory {

		private final Context mContext;

		public TabFactory(Context context) {
			mContext = context;
		}

		public View createTabContent(String tag) {
			View v = new View(mContext);
			return v;
		}
	}

	private View getTabIndicator(Context context, String title, int icon) {
		View view = LayoutInflater.from(context).inflate(R.layout.tab_indicator, null);
		ImageView iv = (ImageView) view.findViewById(R.id.icon);
		iv.setImageResource(icon);
		return view;
	}

	@Override
	public void onTabChanged(String tabId) {
		int pagerNumber = 0;
		if (tabId.equalsIgnoreCase(getString(R.string.title_miss_call))) {
			pagerNumber = 0;
		} else if (tabId.equalsIgnoreCase(getString(R.string.title_about))) {
			pagerNumber = 1;
		} else if (tabId.equalsIgnoreCase(getString(R.string.title_contact))) {
			pagerNumber = 2;
		} else {
			pagerNumber = 3;
		}

		mViewPager.setCurrentItem(pagerNumber);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int position) {
		mTabHost.setCurrentTab(position);
	}
}
